<?php

declare(strict_types=1);

namespace app\admin\controller\sys;

use app\admin\controller\sys\business\AdminNodeBusiness;
use app\admin\controller\sys\business\AdminUserBusiness;
use app\admin\controller\sys\model\AdminUserModel;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
use think\response\Json;
use think\response\Redirect;

/**
 * 后台主页
 * Class Index
 * @package app\admin\controller\sys
 */
class Index extends BaseController
{
    /**
     * 主页
     * @return string
     */
    public function index()
    {
        $role = AdminUserModel::find($this->request->admin_user_id)->adminRole;
        View::assign('role', $role);
        $node = AdminNodeBusiness::getTree($this->request->menu);
        View::assign('node', $node);
        return View::fetch(app()->getAppPath() . 'controller/sys/view/index/index.html');
    }



    /**
     * 控制台
     * @return string
     */
    public function console()
    {
        return View::fetch(app()->getAppPath() . 'controller/sys/view/index/console.html');
    }


    /**
     * 退出登录
     * @return Json
     */
    public function logout(): Json
    {
        session(config('admin.admin_session'), null);
        session(config('admin.role_session'), null);
        session(config('admin.node_session'), null);
        session(config('admin.access_session'), null);
        return success('退出成功！', [], (string)url('sys.login/index'));
    }

    /**
     * 清理数据表
     * @return Json
     */
    public function clearAll()
    {
        Db::startTrans();
        try {
            //截断相关表（文件角色权限等）
            $table_truncate_arr = [
                'db_admin_access',
                'db_admin_file',
                'db_admin_log',
                'db_admin_role',
                'db_admin_role_access',
                'db_admin_role_node',
                'db_admin_role_user',
            ];

            foreach ($table_truncate_arr as $table_name) {
                $sql = 'truncate '. $table_name . ';';
                Db::execute($sql);
            }

            //除了admin第一条外 截断admin_user表
            $user_admin = Db::name('admin_user')->where('id', 1)->find();
            unset($user_admin['id']);
            Db::execute('truncate db_admin_user');
            Db::name('admin_user')->save($user_admin);

            //admin_node表保留默认
            $default_node = Db::name('admin_node')->where('id', '<', 6)->select()->toArray();
            Db::execute('truncate db_admin_node');
            foreach ($default_node as $node) {
                unset($node['id']);
                Db::name('admin_node')->save($node);
            }
            Db::commit();
            return success('success');
        } catch (\Exception $e) {
            Db::rollback();
            return error($e->getMessage());
        }
    }

    /**
     *  更换角色
     * @return Json
     */
    public function changeRole(): Json
    {
        $role_id = input('role_id', 0);
        try {
            (new \app\admin\service\AdminUser())->changeRole($this->request->admin_user_id, $role_id);
            return success('更换角色成功');
        } catch (\Exception $e) {
            return error($e->getMessage());
        }
    }
}
